package hauwei.exam416;

import java.util.*;

public class DIP {
    static class IPRange {
        String start;
        String end;
        long startValue;
        long endValue;
        long length;

        public IPRange(String start, String end) {
            this.start = start;
            this.end = end;
            this.startValue = ipToLong(start);
            this.endValue = ipToLong(end);
            this.length = endValue - startValue + 1;
        }

        private long ipToLong(String ip) {
            String[] ipParts = ip.split("\\.");
            long result = 0;
            for (int i = 0; i < 4; i++) {
                result = result << 8 | Integer.parseInt(ipParts[i]);
            }
            return result;
        }

        @Override
        public String toString() {
            return start + " " + end;
        }
    }

    static class Selector{
        public long startValue;
        public long endValue;
        public int taskNum;
        public long length;
        public List<IPRange> list;
        Selector(long s,int t,long l,long e){
            startValue=s;
            taskNum=t;
            length=l;
            endValue=e;
            list=new ArrayList<>();
        }
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        sc.nextLine();

        List<IPRange> ranges=new ArrayList<>();
        for(int i=0;i<n;i++){
            String[]input=sc.nextLine().split(" ");
            ranges.add(new IPRange(input[0],input[1]));
        }

        Collections.sort(ranges, Comparator.comparingLong(r->r.endValue));

        Selector[]dp=new Selector[ranges.size()];
        for(int i=0;i<dp.length;i++){
            IPRange ipRange = ranges.get(i);
            dp[i]=new Selector(ipRange.startValue,1,ipRange.length,ipRange.endValue);
            for(int j=i-1;j>=0;j--){
                if(dp[i].startValue>dp[j].endValue&&dp[i].taskNum<=dp[j].taskNum+1){ //如果大于
                    dp[i]=new Selector(ipRange.startValue,
                            dp[j].taskNum+1,
                            dp[j].length+ipRange.length,
                            ipRange.endValue
                            );
                    Selector selector = dp[j];
                    ArrayList<IPRange> ipRanges = new ArrayList<>(selector.list);
                    ipRanges.add(ipRange);
                    if(dp[i].length==dp[j].length&&dp[i].startValue>=dp[j].startValue){
                        dp[i]=new Selector(ipRange.startValue,
                                dp[j].taskNum+1,
                                dp[j].length+ipRange.length,
                                ipRange.endValue
                        );
                    }
                }
            }
        }
        int maxTaskNum=dp[dp.length-1].taskNum;
        int j=dp.length-1;
        while(j>=0&&dp[dp.length-1].taskNum==maxTaskNum){
            j--;
        }
        System.out.println();
    }
}
